<?php defined('SYSPATH') or die('No direct script access.');

class Request extends Kohana_Request {
	
	public static function factory($uri = TRUE, Cache $cache = NULL, $injected_routes = array())
	{
		// If this is the initial request
		if ( ! Request::$initial)
		{
			if (Kohana::$is_cli)
			{
				// Default protocol for command line is cli://
				$protocol = 'cli';
	 
				// Get the command line options
				$options = CLI::options('uri', 'method', 'get', 'post', 'referrer');
	 
				if (isset($options['uri']))
				{
					// Use the specified URI
					$uri = $options['uri'];
				}
	 
				if (isset($options['method']))
				{
					// Use the specified method
					$method = strtoupper($options['method']);
				}
				else
				{
					// Default to GET requests
					$method = HTTP_Request::GET;
				}
	 
				if (isset($options['get']))
				{
					// Overload the global GET data
					parse_str($options['get'], $_GET);
				}
	 
				if (isset($options['post']))
				{
					// Overload the global POST data
					parse_str($options['post'], $_POST);
				}
	 
				if (isset($options['referrer']))
				{
					$referrer = $options['referrer'];
				}
			}
			else
			{
				//post 由json转为array
				if (empty($_POST))
				{	
					$_POST = json_decode(file_get_contents("php://input"), true);
				}
				if (isset($_SERVER['REQUEST_METHOD']))
				{
					// Use the server request method
					$method = $_SERVER['REQUEST_METHOD'];
				}
				else
				{
					// Default to GET requests
					$method = HTTP_Request::GET;
				}
	 
				if ( ! empty($_SERVER['HTTPS']) AND filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN))
				{
					// This request is secure
					$protocol = 'https';
				}
				else
				{
					$protocol = 'http';
				}
	 
				if (isset($_SERVER['HTTP_REFERER']))
				{
					// There is a referrer for this request
					$referrer = $_SERVER['HTTP_REFERER'];
				}
	 
				if (isset($_SERVER['HTTP_USER_AGENT']))
				{
					// Browser type
					Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
				}
	 
				if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
				{
					// Typically used to denote AJAX requests
					$requested_with = $_SERVER['HTTP_X_REQUESTED_WITH'];
				}
	 
				if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
				{
					// Use the forwarded IP address, typically set when the
					// client is using a proxy server.
					Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
				}
				elseif (isset($_SERVER['HTTP_CLIENT_IP']))
				{
					// Use the forwarded IP address, typically set when the
					// client is using a proxy server.
					Request::$client_ip = $_SERVER['HTTP_CLIENT_IP'];
				}
				elseif (isset($_SERVER['REMOTE_ADDR']))
				{
					// The remote IP address
					Request::$client_ip = $_SERVER['REMOTE_ADDR'];
				}
	 
				if ($method !== 'GET')
				{
					// Ensure the raw body is saved for future use
					$body = file_get_contents('php://input');
				}
	 
				if ($uri === TRUE)
				{
					// Attempt to guess the proper URI
					$uri = Request::detect_uri();
				}
			}
			

			// Create the instance singleton
			Request::$initial = $request = new Request($uri, $cache);
	 
			// Store global GET and POST data in the initial request only
			$request->query($_GET);
			$request->post($_POST);
	 
			if (isset($protocol))
			{
				// Set the request protocol
				$request->protocol($protocol);
			}
	 
			if (isset($method))
			{
				// Set the request method
				$request->method($method);
			}
	 
			if (isset($referrer))
			{
				// Set the referrer
				$request->referrer($referrer);
			}
	 
			if (isset($requested_with))
			{
				// Apply the requested with variable
				$request->requested_with($requested_with);
			}
	 
			if (isset($body))
			{
				// Set the request body (probably a PUT type)
				$request->body($body);
			}
		}
		else
		{
			$request = new Request($uri, $cache, $injected_routes);
		}
	 
		return $request;
	}
	
	
	
}
